package com.scott.crm.student.controller;


import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.base.utils.AESUtil;
import com.scott.crm.clazz.bean.ClazzBean;
import com.scott.crm.student.bean.DictionaryBean;
import com.scott.crm.student.bean.StudentBean;
import com.scott.crm.student.bean.StudentDto;
import com.scott.crm.student.entity.StudentEntity;
import com.scott.crm.student.service.StudentService;
import com.scott.crm.user.bean.UserBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/student")
@CrossOrigin
@RestController
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;


    @RequestMapping(value = "/findClassByStudentID",method = RequestMethod.GET)
    public DataRes findClassByStudentID(Integer studentId) throws Exception{
        ClazzBean clazz = studentService.findClassByStudentID(studentId);
        if (clazz==null){
            return DataUtil.errors();
        }else {
            return DataUtil.success(clazz);
        }
    }


    @RequestMapping(value = "/findTeacherByClazzID",method = RequestMethod.GET)
    public DataRes findTeacherByStudentID(Integer classId) throws Exception{
        UserBean teacher = studentService.findTeacherByClazzID(classId);
        if (teacher==null){
            return DataUtil.errors();
        }else {
            return DataUtil.success(teacher);
        }
    }



    @RequestMapping(value = "/StudentMessage",method = RequestMethod.GET)
    public DataRes findStudentMessage(String token) throws Exception{
        // 解密拿到userId
        String studentId = AESUtil.decrypt(token, AESUtil.KEY);
        StudentEntity studentMessage = studentService.findStudentMessageByStudentId(Integer.parseInt(studentId));
        return DataUtil.success(studentMessage);
    }


    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    public DataRes addStudent(@RequestBody StudentBean studentBean) throws Exception{
        studentService.addStudent(studentBean);
      return DataUtil.success(null);
    }



    @RequestMapping(value = "/updateUserMessage",method = RequestMethod.POST)
    public DataRes updateUserMessage(@RequestBody StudentEntity studentEntity) throws Exception{
        studentService.updateUserMessage(studentEntity);
        return DataUtil.success(null);
    }

    @RequestMapping(value = "/findAllDictionary",method = RequestMethod.POST)
    public DataRes findEducation() throws Exception{
        List<DictionaryBean> Dictionary = studentService.findEducation();
        return DataUtil.success(Dictionary);
    }

    @RequestMapping(value = "/findClassAndTeacherByStudentId",method = RequestMethod.GET)
    public DataRes findClassAndTeacherByStudentId(String token) throws Exception{

        StudentDto studentDto = studentService.findClassAndTeacherByStudentId(token);
        return DataUtil.success(studentDto);
    }





}
